import Layer from "src/components/Layer"
import navigationhelper from "src/components/navigationhelper"
import {AsyncStorage} from "react-native"


function requestPromise(payload = {}) {
    var method = payload.method || 'POST'
    if (method === 'POST') {
        return Post(payload)
    }
    return Get(payload)
}

function requestUpload(payload = {}) {
    return fetch(payload.uri, {
        method: 'POST',
        body: payload.form,
        headers: {
            'Content-Type': 'multipart/form-data',
        }
    }).then((response) =>{
        return response.json()
    }).then(responseJson => {
        if(responseJson.status === 'success'){
            return responseJson
        }

        throw {message: responseJson.failedMsg, code: responseJson.failedCode}
    }).catch((error) => {
        if(error.code == 'NO_LOGIN'){
            AsyncStorage.removeItem('token')
            AsyncStorage.removeItem('currentUser')
            navigationhelper.navigate('Login')
        }else if (error.code == 'NO_REALNAME') {
           navigationhelper.navigate('UserIdentity')
        }
        Layer.showMsg('图片识别失败，请上传正确且清晰的照片')
        throw error
    })
}



function Post(payload) {
    return fetch(payload.uri, {
        method: "POST",
        mode: "cors",
        headers: {
            Accept: "application/json",
            "Content-Type": "application/json",
            Connection: "close",
            type: "getUserData"
        },
        body: JSON.stringify(payload.body || {})
    }).then(response => {
        if (response.ok) {
            return response.json();
        }
        return response;
    }).then(responseJson => {
        if(responseJson.status === 'success'){
            return responseJson
        }
        throw {message: responseJson.failedMsg, code: responseJson.failedCode}
    }).catch(error => {
        if(error.code == 'NO_LOGIN'){
            AsyncStorage.removeItem('token')
            AsyncStorage.removeItem('currentUser')
            navigationhelper.navigate('Login')
        }else if (error.code == 'NO_REALNAME') {
            navigationhelper.navigate('UserIdentity')
        }
        throw error
    })
}


function Get(payload) {
    return fetch(payload.uri, {
        method: "GET",
        headers: {
            Accept: "application/json",
            "Content-Type": "application/json"
        }
    }).then(response => {
        if (response.ok) {
            return response.json();
        }
        return response;
    }).then(responseJson => {
        if(responseJson.status === 'success'){
            return responseJson
        }
        throw {message: responseJson.failedMsg, code: responseJson.failedCode}
    }).catch(error => {
        if(error.code == 'NO_LOGIN'){
            AsyncStorage.removeItem('token')
            AsyncStorage.removeItem('currentUser')
            navigationhelper.navigate('Login')
        }else if (error.code == 'NO_REALNAME') {
            // navigationhelper.navigate('UserIdentity')
        }
        throw error
    })
}

function getParams(payload) {
    var uri = ''
    Object.keys(payload).forEach(function (key) {
        uri = uri + key + '=' + payload[key] + '&'
    })
    if(uri.length > 0) {
        uri = uri.substring(0, uri.length - 1)
    }
    return uri
}

module.exports = {
    requestPromise: requestPromise,
    requestUpload: requestUpload,
    getParams: getParams
}